// pages/workPlan/daka/index.js
import request from '../../../utils/request';
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    planId: "1",
    planName: "张三的打卡计划",
    workItemId: "9",
    workItemName: "背诵课文",
    type: 'begin',
    time: 0,
    status: '',
    extraScore: 0,
    isParent: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const app = getApp();
    let globalData = app.globalData;

    console.log(options)
    var qsrq = options.beginTime;
	 
    let beginTime = new Date(qsrq.replace(/-/g,"/").replace(/T/g, " "));//开始日期
    
    let nowTime = new Date()
    console.log(Number(options.planCost), nowTime.getTime()-beginTime.getTime());
    this.setData({
      planId: options.planId,
      planName: options.planName,
      workItemId: options.workItemId,
      workItemName: options.workItemName,
      remark: options.remark,
      status: options.status,
      time: Number(options.planCost)* 60 * 1000 - (nowTime.getTime()-beginTime.getTime()),
      isParent: globalData.isParent
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getStatus();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  begin() {
    const {
      planId,
      workItemId
    } = this.data;
    request({
      url: 'workPlan/addRecord',
      data: {
        planId: planId,
        planItemId: workItemId,
        type: '1'
      },
      success: (res) => {
        // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
        console.log('workPlan/addRecord', res)
        Toast.success(res.msg);
        this.setData({
          status: '1',
          time: Number(this.planCost) * 60 *1000
        })
      }
    })
  },
  end() {
    const {
      planId,
      workItemId
    } = this.data;
    request({
      url: 'workPlan/addRecord',
      data: {
        planId,
        planItemId: workItemId,
        type: '2'
      },
      success: (res) => {
        // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
        console.log('workPlan/addRecord', res)
        Toast.success(res.msg);
        this.setData({
          status: '2'
        })
      }
    })
  },
  getStatus() {
    const {
      planId,
      workItemId
    } = this.data;
    request({
      url: 'workPlan/toAddRecord',
      data: {
        planId,
        planItemId: workItemId,
        type: '1'
      },
      success: (res) => {
        // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
        console.log('workPlan/toAddRecord', res)
        Toast.success(res.msg);
      }
    })
  },
  addSore() {
    console.log(this.data.extraScore)
    console.log(Number(this.data.extraScore))
    const {
      planId,
      workItemId,
      extraScore,
      workItemName
    } = this.data;
    console.log(planId,
      workItemId,
      extraScore,
      workItemName)
    request({
      url: 'userItemRecord/add',
      data: {
        planId: planId,
        planItemId: workItemId,
        score: extraScore,
        itemName: workItemName
      },
      success: (res) => {
        // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
        console.log('userItemRecord/add', res)
        Toast.success(res.msg);
        this.getStatus();
      }
    })

  }
})